package com.java1234.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.java1234.model.Student;
import com.java1234.service.StudentService;

@org.springframework.stereotype.Controller
public class Controller {

	@Resource
	private StudentService studentService;

	@RequestMapping("list")
	public String list(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException {

		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		List<Student> list = studentService.findStudents();
		request.setAttribute("students", list);

		return "list";
	}

	@RequestMapping("toAdd")
	public String toAdd(HttpServletRequest request) {

		return "add";
	}

	@RequestMapping("toUpdate/{id}")
	public String toUpdate(HttpServletRequest request,
			@PathVariable("id") int id) {

		return "update";
	}

	@RequestMapping("add")
	public void add(HttpServletRequest request, HttpServletResponse response)
			throws IOException {

		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
//		int id = Integer.valueOf(request.getParameter("id"));
		String name = request.getParameter("name");
		int age = Integer.valueOf(request.getParameter("age"));

		Student student = new Student();
//		student.setId(id);
		student.setName(name);
		student.setAge(age);

		studentService.addStudent(student);
		response.sendRedirect("list.do");

	}
	
	
	@RequestMapping("update")
	public void update(HttpServletRequest request, HttpServletResponse response)
			throws IOException {

		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		int id = Integer.valueOf(request.getParameter("id"));
		String name = request.getParameter("name");
		int age = Integer.valueOf(request.getParameter("age"));

		Student student = new Student();
		student.setId(id);
		student.setName(name);
		student.setAge(age);

		studentService.updateStudent(student);
		response.sendRedirect("list.do");

	}

	@RequestMapping("get")
	public String get(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		int id = Integer.parseInt(request.getParameter("id"));

		/*
		 * Student student = new Student(); student.setId(id);
		 * student.setName(name); student.setAge(age);
		 */
		Student student = studentService.getStudent(id);
		request.setAttribute("stu", student);
		return "update";

	}


	@RequestMapping("delete")
	public void delete(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		int id = Integer.valueOf(request.getParameter("id"));

		studentService.deleteStudent(id);
		response.sendRedirect("list.do");

	}
}
